<template>
  <div class="app-container">
    <el-tabs type="border-card">
        <!-- 费用设置 -->
      <el-tab-pane label="费用设置">
        <div class="costBtn">
          <div class="areasBtn" @click="costBtnFn('1')">
            <img v-if="costtypeObj.areas=='1'" src="@/assets/cost/areas_active.png" alt="">
            <img v-else src="@/assets/cost/areas.png" alt="">
          </div>
          <div class="timesBtn" @click="costBtnFn('2')">
            <img v-if="costtypeObj.times=='2'" src="@/assets/cost/times_active.png" alt="">
            <img v-else src="@/assets/cost/times.png" alt="">
          </div>
        </div>
        <div class="costTypeCon" style="padding:10px 20px;color:red;font-size:18px;">
            当前计费类型为：{{costType==1?"面积":"次数"}}
        </div>
        <div class="costTypeCon">
          <el-form :model="costFrom" ref="costFrom" :inline="true" label-width="110px">
            <el-form-item label="面积每平米单价" prop="areaPrice" v-if="costType==1">
            <el-input style="width: 176px;"
              v-model="costFrom.areaPrice"
              placeholder="请输入面积每平米单价"
              clearable
              size="small"
            />
            </el-form-item>
            <el-form-item label="每次单价" prop="timePrice" v-if="costType !=1">
             <el-input style="width: 176px;"
              v-model="costFrom.timePrice"
              placeholder="请输入每次单价"
              clearable
              size="small"
            />
          </el-form-item>
          </el-form>
        </div>
        <div class="costTypeCon">
          <p v-if="costType==1">
            按面积收费规则：<br>
            1.设备注册时选择的城市为收费计算区域基准参数。<br>
            2.收费计算公式：费用=户型图总面积*设备所在城市1㎡计费单价。
          </p>   
          <p v-else>
            按次数收费规则：<br>
            1.设备注册时选择的城市为收费计算区域基准参数。<br>
            2.收费计算公式：费用=打印次数*设备所在城市单次计费单价。
          </p>
          <div style="margin-left: 548px"> 
           <el-button type="primary" size="mini" @click="handleQuerySubmit">确 定</el-button>
          </div>
        </div>
      </el-tab-pane>
      <!-- 费用规则 -->
      <el-tab-pane label="费用规则">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
          <el-form-item label="省/市">
            <div class="block">
                <el-cascader style="width:350px"
                  v-model="queryStr.strs"
                  :options="areaList"
                  @keyup.enter.native="handleQuery">
                </el-cascader>
              </div>
          </el-form-item>
         <!--  <el-form-item label="区县id" prop="countyId">
            <el-input
              v-model="queryParams.countyId"
              placeholder="请输入区县id"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="面积每平米单价" prop="areaPrice">
            <el-input
              v-model="queryParams.areaPrice"
              placeholder="请输入面积每平米单价"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="每次单价" prop="timePrice">
            <el-input
              v-model="queryParams.timePrice"
              placeholder="请输入每次单价"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item> -->
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['cost:costRules:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['cost:costRules:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['cost:costRules:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['cost:costRules:export']"
            >导出</el-button>
          </el-col>
        </el-row>
        <el-table v-loading="loading" :data="costRulesList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="序号" align="center" prop="id" />
          <el-table-column label="省" align="center" prop="pname" />
          <el-table-column label="市区" align="center" prop="cname" />
      <!--     <el-table-column label="区县id" align="center" prop="countyId" /> -->
          <el-table-column label="面积每平米单价" align="center" prop="areaPrice" />
          <el-table-column label="每次单价" align="center" prop="timePrice" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['cost:costRules:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['cost:costRules:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>        
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
        <!-- 添加或修改费用规则对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-form-item label="城市"  prop="strs">
              <div class="block">
                <el-cascader style="width:350px"
                  v-model="form.strs"
                  :options="areaList"
                  @change="handleChange">
                </el-cascader>
              </div>
            </el-form-item>
            <el-form-item label="面积每平米单价" prop="areaPrice">
              <el-input v-model="form.areaPrice" placeholder="请输入面积每平米单价" />
            </el-form-item>
            <el-form-item label="每次单价" prop="timePrice">
              <el-input v-model="form.timePrice" placeholder="请输入每次单价" />
            </el-form-item>
           
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </el-tab-pane>

    </el-tabs>
  </div>
</template>

<script>
import { listCostRules, getCostRules, delCostRules, addCostRules, updateCostRules, exportCostRules,updateCostDefault } from "@/api/cost/costRules";
import { areaList } from "@/store/areaSimple"

export default {
  name: "CostRules",
  data() {
    return {
      // 遮罩层
      loading: true,

      areaList: areaList,
      // 选中数组
      ids: [],

      costFrom:{
        dictCodeArea:undefined,
        dictCodeTimes:undefined,
        costTypeCode:undefined,
        costType: undefined,
        areaPrice: undefined,
        timePrice: undefined,
      },
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,

      queryStr:{
        str:undefined,
      },
      costTypes: [],

      costArea: [],

      costTimes: [],
      // 费用规则表格数据
      costRulesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        provinceId: undefined,
        cityId: undefined,
        countyId: undefined,
        areaPrice: undefined,
        timePrice: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        areaPrice: [
          { required: true, message: "面积每平米单价不能为空", trigger: "blur" }
        ],
        timePrice: [
          { required: true, message: "每次单价不能为空", trigger: "blur" }
        ],
         strs: [
          { required: true, message: "请选择城市", trigger: "blur" }
        ],
      },
      costType: '1',
      costtypeObj: {
        areas: "1",
        times: ""
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_cost_rules").then(response => {
      this.costTypes = response.data;
      console.log(this.costTypes)
      this.costFrom.timePrice = response.data[1].dictValue;
      this.costFrom.areaPrice = response.data[0].dictValue;
      this.costFrom.costType = response.data[2].dictValue;
      this.costFrom.dictCodeArea = response.data[0].dictCode;
      this.costFrom.dictCodeTimes = response.data[1].dictCode;
      this.costFrom.costTypeCode = response.data[2].dictCode;
      const typeval = this.costFrom.costType;
      this.costType = typeval;
      if(typeval == "1"){
        this.costtypeObj = {
          areas: "1",
          times: ""
        }
      }else{
        this.costtypeObj = {
          areas: "",
          times: "2"
        }
      }

    });
  },
  methods: {
    /** 查询费用规则列表 */
    getList() {
      this.loading = true;
      if(this.queryStr.strs!=null && this.queryStr.strs.length==2){
        this.queryParams.provinceId= this.queryStr.strs[0]
        this.queryParams.cityId= this.queryStr.strs[1]

        }
      listCostRules(this.queryParams).then(response => {
        this.costRulesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
     handleChange(value) {
        this.form.strs = value
        if(this.form.strs!=null && this.form.strs.length==2){
        this.form.provinceId= this.form.strs[0]
        this.form.cityId= this.form.strs[1]
        }   
    },
    
    /** 确认 */
    handleQuerySubmit: function() {
      this.costFrom.costType = this.costType ;
      if(this.costFrom.costType === "1"){
        if( this.costFrom.areaPrice === ""){
            return this.msgSuccess("请填写默认计费值");
          }
          this.costFrom.timePrice = null;
          this.costFrom.dictCodeTimes = null;
      }else{
        if( this.costFrom.timePrice === ""){
          return this.msgSuccess("请填写默认计费值");
        }
        this.costFrom.areaPrice = null;
        this.costFrom.dictCodeArea = null;
      } 
      updateCostDefault(this.costFrom).then(response => {
        if (response.code == 0) {
          this.msgSuccess("计费规则修改成功");
          this.getDicts("sys_cost_rules").then(response => {
            this.costTypes = response.data;
            console.log(this.costTypes)
            this.costFrom.timePrice = response.data[1].dictValue;
            this.costFrom.areaPrice = response.data[0].dictValue;
            this.costFrom.costType = response.data[2].dictValue;
            this.costFrom.dictCodeArea = response.data[0].dictCode;
            this.costFrom.dictCodeTimes = response.data[1].dictCode;
            this.costFrom.costTypeCode = response.data[2].dictCode;
          });
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        provinceId: undefined,
        cityId: undefined,
        countyId: undefined,
        areaPrice: undefined,
        timePrice: undefined,
        createBy: undefined,
        createTime: undefined,
        delFlag: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      //this.resetForm("queryForm");
      this.areaList = [];
      this.queryParams.pageNum = 1;
      this.queryParams.provinceId = undefined;
      this.queryParams.cityId = undefined;
      this.queryParams.pageSize = 10;
      listCostRules(this.queryParams).then(response => {
        this.costRulesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加费用规则";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCostRules(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改费用规则";
        this.form.strs=[];
        this.form.strs.push(String(this.form.provinceId));
        this.form.strs.push(String(this.form.cityId));
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateCostRules(this.form).then(response => {
              if (response.code == 0) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addCostRules(this.form).then(response => {
              if (response.code == 0) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除费用规则编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCostRules(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有费用规则数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCostRules(queryParams);
        }).then(response => {
          this.download(response.data);
        }).catch(function() {});
    },
    /** 费用类型单击事件 */
    costBtnFn(typeval) {
      this.costType = typeval;
      if(typeval == "1"){
        this.costtypeObj = {
          areas: "1",
          times: ""
        }
      }else{
        this.costtypeObj = {
          areas: "",
          times: "2"
        }
      }
    }
  }
};
</script>

<style scoped>
  .costTypeCon{
    width: 90%;
    margin: 0 auto;
    padding: 20px;
    color: #666666;
    line-height: 30px;
  }
  .costBtn{
    display: flex;
    width: 90%;
    margin: 0 auto;
    padding: 0 20px;
  }
  .costBtn div{
    margin-right: 30px;
    cursor: pointer;
  }
</style>